const logger = require('log4js').getLogger()
const Wechat = require('../util/Wechat')

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, (reqObj, resolve) => {
        const mongoMng = require('../mng/mongoMng')
        if (!mongoMng.db) {
            logger.error('[' + __filename + ':' + __line + ']', '未连接mongo')
            resolve({
                result: -1,
                msg: '系统错误'
            })
            return;
        }
        if (!reqObj.phone) {
            resolve({
                result: -1,
                msg: '缺少phone'
            })
            return
        }
        // if (!reqObj.password) {
        //     resolve({
        //         result: -1,
        //         msg: '缺少password'
        //     })
        //     return
        // }
        if (!reqObj.returnWay) {
            resolve({
                result: -1,
                msg: '缺少returnWay'
            })
            return
        }
        if (reqObj.returnWay == 'alipay') {
            if (!reqObj.alipay) {
                resolve({
                    result: -1,
                    msg: '缺少alipay'
                })
                return
            }
        }
        if (!reqObj.code) {
            resolve({
                result: -1,
                msg: '缺少code'
            })
            return
        }
        var date = new Date()
        date.setTime(date.getTime() - 1800000)
        mongoMng.db.collection('code').find({
            phone: reqObj.phone,
            code: reqObj.code,
            time: {
                $gte: require('../../lib/formatDate')(date)
            }
        }).toArray((err, result) => {
            if (err) {
                logger.error('[' + __filename + ':' + __line + ']', err)
                resolve({
                    result: -1,
                    msg: '系统错误'
                })
                return;
            }
            if (result.length == 0) {
                resolve({
                    result: -1,
                    msg: '手机号或验证码错误'
                })
                return
            }
            mongoMng.db.collection('user').find({
                phone: reqObj.phone
            }).toArray((err, result) => {
                if (err) {
                    logger.error('[' + __filename + ':' + __line + ']', err)
                    return;
                }
                if (result.length == 0) {
                    var doc = {
                        phone: reqObj.phone,
                        returnWay: reqObj.returnWay,
                        registerTime: new Date()
                    }
                    if (reqObj.password) {
                        doc,password = reqObj.password
                    }
                    if (reqObj.alipay) {
                        doc.alipay = reqObj.alipay
                    }
                    if (reqObj.promoter) {
                        doc.promoter = reqObj.promoter
                    }
                    if (req.session.wechat) {
                        logger.debug('[' + __filename + ':' + __line + ']', 'phone:', reqObj.phone, 'old whchat:', '', 'new wehcat:', req.session.wechat.openid)
                        doc.wechat = req.session.wechat.openid
                    }
                    mongoMng.db.collection('user').insert(doc, (err, result) => {
                        if (err) {
                            logger.error('[' + __filename + ':' + __line + ']', err)
                            return
                        }
                        if (req.session.wechat) {
                            Wechat({
                                AppID: 'wx226c3c9557c44304',
                                AppSecret: '8bff6155d050947d06bf7802e69f4acb'
                            }).messageCustomSend({
                                touser: req.session.wechat,
                                msgtype: 'text',
                                content: '绑定成功'
                            }).then(result => {
                                logger.debug('[' + __filename + ':' + __line + ']', result)
                            }, result => {
                                logger.error('[' + __filename + ':' + __line + ']', result)
                            })
                        }
                    })
                    require('../util/loginResolve')(doc, req, resolve)
                } else {
                    resolve({
                        result: -1,
                        msg: '手机号已注册'
                    })
                }
            });
        });
    })
}
